/**
  ******************************************************************************
  * @file    rtimer-arch.c
  * @author  Iron
  * @date    2022-01-01
  * @version v1.0
  * @brief   rtimer-arch c file
  */

/* Private includes ----------------------------------------------------------*/
#include "contiki.h"

/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private typedef -----------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
static volatile rtimer_clock_t next_trigger;

/* Private function prototypes -----------------------------------------------*/

/*----------------------------------------------------------------------------*/
void
rtimer_arch_init(void)
{
    return;
}

/*---------------------------------------------------------------------------*/
void
rtimer_arch_schedule(rtimer_clock_t t)
{
    next_trigger = t;
}

/*---------------------------------------------------------------------------*/
rtimer_clock_t
rtimer_arch_next_trigger()
{
    return next_trigger;
}

/*---------------------------------------------------------------------------*/
/**
 * \brief Returns the current real-time clock time
 * \return The current rtimer time in ticks
 */
rtimer_clock_t
rtimer_arch_now()
{
    rtimer_clock_t rv = 0;

    /* SMWDTHROSC_ST0 latches ST[1:3] and must be read first */
//    rv = REG(SMWDTHROSC_ST0);
//    rv |= (REG(SMWDTHROSC_ST1) << 8);
//    rv |= (REG(SMWDTHROSC_ST2) << 16);
//    rv |= (REG(SMWDTHROSC_ST3) << 24);

    return rv;
}

/*---------------------------------------------------------------------------*/
/**
 * \brief The rtimer ISR
 *
 *        Interrupts are only turned on when we have an rtimer task to schedule
 *        Once the interrupt fires, the task is called and then interrupts no
 *        longer get acknowledged until the next task needs scheduled.
 */
void
rtimer_isr()
{
    /*
     * If we were in PM1+, call the wake-up sequence first. This will make sure
     * that the 32MHz OSC is selected as the clock source. We need to do this
     * before calling the next rtimer_task, since the task may need the RF.
     */
//    lpm_exit();

    next_trigger = 0;

//    NVIC_ClearPendingIRQ(SMT_IRQn);
//    NVIC_DisableIRQ(SMT_IRQn);

    rtimer_run_next();
}

/**
  * @}
  */

/******************* (C)COPYRIGHT 2022 ***** END OF FILE *********************/
